package editor.main;

import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import editor.modelo.EditorFloor;
import java.awt.GridLayout;
import java.awt.FlowLayout;


public class FloorPanel extends JPanel implements EntityEditor<EditorFloor> {

	private static final long serialVersionUID = 2361931850397956122L;
	private EditorFloor entidad;
	private JTextField txtName;
	private JTextField txtX;
	private JTextField txtY;
	private JTextField txtNivel;
	private JTextField txtNodoRef;
	private JTextField txtAltura;
	/**
	 * Create the panel.
	 */
	public FloorPanel() {
		setPreferredSize(new Dimension(158, 156));
		setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));

		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNombre);

		txtName = new JTextField();
		txtName.setRequestFocusEnabled(false);
		txtName.setPreferredSize(new Dimension(20, 20));
		txtName.setEnabled(false);
		add(txtName);
		txtName.setColumns(10);

		JLabel lblNivel = new JLabel("Nivel:");
		lblNivel.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNivel);

		txtNivel = new JTextField();
		txtNivel.setColumns(10);
		add(txtNivel);

		JLabel lblNodoRefnombre = new JLabel("Nodo Ref:");
		lblNodoRefnombre.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblNodoRefnombre);

		txtNodoRef = new JTextField();
		txtNodoRef.setColumns(10);
		add(txtNodoRef);

		JLabel lblAlturaAbs = new JLabel("Altura Abs:");
		lblAlturaAbs.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblAlturaAbs);

		txtAltura = new JTextField();
		txtAltura.setColumns(10);
		add(txtAltura);

		JLabel lblCentroX = new JLabel("Centro X:");
		lblCentroX.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCentroX);

		txtX = new JTextField();
		txtX.setEditable(false);
		txtX.setColumns(10);
		add(txtX);

		JLabel lblCentroY = new JLabel("Centro Y:");
		lblCentroY.setHorizontalAlignment(SwingConstants.RIGHT);
		add(lblCentroY);

		txtY = new JTextField();
		txtY.setEditable(false);
		txtY.setColumns(10);
		add(txtY);
	}

	public void setEntity(EditorFloor entity) {
		this.entidad = entity;
		this.txtName.setText(entidad.getFloorName());
		this.txtNivel.setText(String.valueOf(entidad.getLevel()));
		this.txtAltura.setText(String.valueOf(entity.getAlturaAbsouta()));
		if(entity.getReferencia()!= null)
		this.txtNodoRef.setText(entity.getReferencia().getId());
		//this.comboBoxTipos.setSelectedItem(entity.getTipo());
		this.txtX.setText("TODO"); //String.valueOf(entidad.getFloorSpace().getCentro().getX()));
		this.txtY.setText("TODO"); //String.valueOf(entidad.getCentro().getY()));

	}

	public EditorFloor getEntity() {
		if(entidad.getReferencia() == null)
			JOptionPane.showMessageDialog(this, "cuidado, no se encontro nodo de referencia");
		return entidad;
	}

	public void applyChanges() {
		//	entidad.getCentro().Float.valueOf(txtX));
		entidad.setFloorName(this.txtName.getText());
		try{
			entidad.setLevel(Integer.parseInt(this.txtNivel.getText()));
		}catch(Exception e){
			entidad.setLevel(0);
		}
		try{
			entidad.setAlturaAbsouta(Integer.parseInt(this.txtAltura.getText()));
		}catch(Exception e){
			entidad.setAlturaAbsouta(0);
		}
		entidad.setReferencia(entidad.findSpacebyId(txtNodoRef.getText()));
	}

	public boolean validateEntity() {

		return true;
	}
}
